#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
import RandomUtils
import StateTransition
from Enums import EStateElementType
from State import State

import state_transition_test_utils
from DV.gpgpu.trees.instruction_tree import BranchJump_map
from base.Sequence import Sequence
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU


# This test verifies that StateTransitions yield the expected State with
# interleaved branch instructions.
class MainSequence(Sequence):
    def __init__(self, aGenThread, aName=None):
        super().__init__(aGenThread, aName)

        self._mExpectedStateData = {}

    def generate(self, **kargs):
        for _ in range(3):
            state = self._createState()

            self._genRandomBranchInstructions()

            StateTransition.transitionToState(state)
            state_transition_test_utils.verify_state(self, self._mExpectedStateData)

            self._genRandomBranchInstructions()

    # Generate a random number of random branch instructions.
    def _genRandomBranchInstructions(self):
        for _ in range(RandomUtils.random32(100, 200)):
            instr = BranchJump_map.pick(self.genThread)
            self.genInstruction(instr)

    # Create a random State to test an explicit StateTransition.
    def _createState(self):
        state = State()
        test_utils = state_transition_test_utils
        self._mExpectedStateData[
            EStateElementType.Memory
        ] = test_utils.add_random_memory_state_elements(self, state, RandomUtils.random32(0, 20))
        self._mExpectedStateData[EStateElementType.GPR] = test_utils.add_random_gpr_state_elements(
            self, state, RandomUtils.random32(0, 20)
        )
        self._mExpectedStateData[
            EStateElementType.FloatingPointRegister
        ] = test_utils.add_random_floating_point_register_state_elements(
            self, state, RandomUtils.random32(0, 20)
        )
        self._mExpectedStateData[EStateElementType.PC] = test_utils.add_random_pc_state_element(
            self, state
        )

        return state


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
